﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace AlarmClock
{  
    public partial class FrmSetTime : Form
    {   
        public FrmSetTime()
        {
            InitializeComponent();
        }

        public DataBase database{set;get;}
        private List<Alarm> alarmList = null;

        private DateTimePicker[] dateTP;
        private GroupBox[] groupBox;
        private NumericUpDown[] numUpdown_h;
        private NumericUpDown[] numUpdown_m;
        private Label[] labelTime;
        private ComboBox[] song;
        private CheckBox[] setT;
        private int[] time=new int[5];

        private void SetTime_Load(object sender, EventArgs e)
        {
            int count = 0;
            string[] musicnames = database.getMusicNameList();
            count = musicnames.Count();
            int i = 0;
            for (; i < count; i++)
            {
                song1.Items.Add(musicnames[i]);
                song2.Items.Add(musicnames[i]);
                song3.Items.Add(musicnames[i]);
                song4.Items.Add(musicnames[i]);
                song5.Items.Add(musicnames[i]);
            }
            dateTP = new DateTimePicker[] { dateTP1, dateTP2, dateTP3, dateTP4, dateTP5 };
            groupBox = new GroupBox[] { groupBox1, groupBox2, groupBox3, groupBox4, groupBox5 };
            numUpdown_h = new NumericUpDown[] { h1, h2, h3, h4, h5 };
            numUpdown_m = new NumericUpDown[] { m1, m2, m3, m4, m5 };
            labelTime = new Label[] { t1, t2, t3, t4, t5 };
            song = new ComboBox[] { song1, song2, song3, song4, song5 };
            setT = new CheckBox[] { SetT1, SetT2, SetT3, SetT4, SetT5 };

            alarmList = database.getAlarmList();
            Alarm alarm = null;
            count = alarmList.Count();
            if (count < 1)
            {
                for (i = 0; i < 5; i++)
                    dateTP[i].MinDate = DateTime.Now;
            }
            else
            {
                for (i = 0; i < count; i++)
                {
                    alarm = alarmList.ElementAt(i);
                    dateTP[i].MinDate = alarm.date;
                    numUpdown_h[i].Value = Convert.ToDecimal(alarm.time.Substring(0, 2));
                    numUpdown_m[i].Value = Convert.ToDecimal(alarm.time.Substring(3, 2));
                    groupBox[i].Enabled = true;
                    song[i].SelectedIndex = (alarm.musicId >= musicnames.Length ? -1 : alarm.musicId);
                    if (alarm.musicId >= 0 && alarm.musicId < musicnames.Length) song[i].Text = musicnames[alarm.musicId];
                    setT[i].Checked = true;
                    time[i] = 1;                
                }
                if(i<5)groupBox[i].Enabled = true;
                for (; i < 5; i++)
                {
                    dateTP[i].MinDate = dateTP[count - 1].MinDate;
                }
            }
        }

        private void dateTP1_ValueChanged(object sender, EventArgs e)
        {
            this.dateTP2.MinDate = this.dateTP1.Value;
            this.dateTP3.MinDate = this.dateTP1.Value;
            this.dateTP4.MinDate = this.dateTP1.Value;
            this.dateTP5.MinDate = this.dateTP1.Value;
        }

        private void dateTP2_ValueChanged(object sender, EventArgs e)
        {
            this.dateTP3.MinDate = this.dateTP2.Value;
            this.dateTP4.MinDate = this.dateTP2.Value;
            this.dateTP5.MinDate = this.dateTP2.Value;
        }

        private void dateTP3_ValueChanged(object sender, EventArgs e)
        {
            this.dateTP4.MinDate = this.dateTP3.Value;
            this.dateTP5.MinDate = this.dateTP3.Value;
        }

        private void dateTP4_ValueChanged(object sender, EventArgs e)
        {
            this.dateTP5.MinDate = this.dateTP4.Value;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            FrmSetTime fr = new FrmSetTime();
            this.Close();
        }

        private void SetT1_CheckedChanged(object sender, EventArgs e)
        {
            if (SetT1.Checked == true)
            {
                groupBox2.Enabled = true;
            }
            else
            {
                if(time[1]!=1)
                    groupBox2.Enabled = false;
            }
        }

        private void SetT2_CheckedChanged(object sender, EventArgs e)
        {
            while (SetT2.Checked == true && ((this.dateTP2.Value == this.dateTP1.Value && this.h2.Value < this.h1.Value) || (this.dateTP2.Value == this.dateTP1.Value && this.h2.Value == this.h1.Value && this.m2.Value < this.m1.Value + 3)))
            {
                DialogResult result;
                result = MessageBox.Show("亲~两个闹铃之间至少要间隔3分钟哦~");
                if (result == DialogResult.OK) SetT2.Checked = false;
                return;
            }
            if (SetT2.Checked == true)
            {
                groupBox3.Enabled = true;
            }
            else
            {
                if (time[2] != 1)
                    groupBox3.Enabled = false;
            }           
        }

        private void SetT3_CheckedChanged(object sender, EventArgs e)
        {
            while (SetT3.Checked == true && ((this.dateTP3.Value == this.dateTP2.Value && this.h3.Value < this.h2.Value) || (this.dateTP3.Value == this.dateTP2.Value && this.h3.Value == this.h2.Value && this.m3.Value < this.m2.Value + 3)))
            {
                DialogResult result;
                result = MessageBox.Show("亲~两个闹铃之间至少要间隔3分钟哦~");
                if (result == DialogResult.OK) SetT3.Checked = false;
                return;
            }
            if (SetT3.Checked == true)
            {
                groupBox4.Enabled = true;
            }
            else
            {
                if (time[3] != 1)
                    groupBox4.Enabled = false;
            }
        }

        private void SetT4_CheckedChanged(object sender, EventArgs e)
        {
            while (SetT4.Checked == true && ((this.dateTP4.Value == this.dateTP3.Value && this.h4.Value < this.h3.Value) || (this.dateTP4.Value == this.dateTP3.Value && this.h4.Value == this.h3.Value && this.m4.Value < this.m3.Value + 3)))
            {
                DialogResult result;
                result = MessageBox.Show("亲~两个闹铃之间至少要间隔3分钟哦~");
                if (result == DialogResult.OK) SetT4.Checked = false;
                return;
            }
            if (SetT4.Checked == true)
            {
                groupBox5.Enabled = true;
            }
            else
            {
                if (time[4] != 1)
                    groupBox5.Enabled = false;
            }
        }

        private void SetT5_CheckedChanged(object sender, EventArgs e)
        {
            while (SetT5.Checked == true && ((this.dateTP5.Value == this.dateTP4.Value && this.h5.Value < this.h4.Value) || (this.dateTP5.Value == this.dateTP4.Value && this.h5.Value == this.h4.Value && this.m5.Value < this.m4.Value + 3)))
            {
                DialogResult result;
                result = MessageBox.Show("亲~两个闹铃之间至少要间隔3分钟哦~");
                if (result == DialogResult.OK) SetT5.Checked = false;
                return;
            }
        }
        
        private void h1_ValueChanged(object sender, EventArgs e)
        {
            String t1Text = "";
            if (h1.Value < 10)
                t1Text = "0";
            t1.Text=t1Text+h1.Value + ":" + t1.Text.Substring(t1.Text.IndexOf(":")+1);
        }

        private void m1_ValueChanged(object sender, EventArgs e)
        {
            String t1Text = t1.Text.Substring(0,3);
            if (m1.Value < 10)
                t1Text += "0";
            t1.Text = t1Text + m1.Value;
        }

        private void h2_ValueChanged(object sender, EventArgs e)
        {
            String t2Text = "";
            if (h2.Value < 10)
                t2Text = "0";
            t2.Text = t2Text + h2.Value + ":" + t2.Text.Substring(t2.Text.IndexOf(":") + 1);
        }

        private void m2_ValueChanged(object sender, EventArgs e)
        {
            String t2Text = t2.Text.Substring(0, 3);
            if (m2.Value < 10)
                t2Text += "0";
            t2.Text = t2Text + m2.Value;
        }

        private void h3_ValueChanged(object sender, EventArgs e)
        {
            String t3Text = "";
            if (h3.Value < 10)
                t3Text = "0";
            t3.Text = t3Text + h3.Value + ":" + t3.Text.Substring(t3.Text.IndexOf(":") + 1);
        }

        private void m3_ValueChanged(object sender, EventArgs e)
        {
            String t3Text = t3.Text.Substring(0, 3);
            if (m3.Value < 10)
                t3Text += "0";
            t3.Text = t3Text + m3.Value;
        }

        private void h4_ValueChanged(object sender, EventArgs e)
        {
            String t4Text = "";
            if (h4.Value < 10)
                t4Text = "0";
            t4.Text = t4Text + h4.Value + ":" + t4.Text.Substring(t4.Text.IndexOf(":") + 1);
        }

        private void m4_ValueChanged(object sender, EventArgs e)
        {
            String t4Text = t4.Text.Substring(0, 3);
            if (m4.Value < 10)
                t4Text += "0";
            t4.Text = t4Text + m4.Value;
        }

        private void h5_ValueChanged(object sender, EventArgs e)
        {
            String t5Text = "";
            if (h5.Value < 10)
                t5Text = "0";
            t5.Text = t5Text + h5.Value + ":" + t5.Text.Substring(t5.Text.IndexOf(":") + 1);
        }

        private void m5_ValueChanged(object sender, EventArgs e)
        {
            String t5Text = t5.Text.Substring(0, 3);
            if (m5.Value < 10)
                t5Text += "0";
            t5.Text = t5Text + m5.Value;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            database.deleteAllAlarm();
            if(SetT1.Checked==true)
            {
                database.addAlarm(this.dateTP1.Value.ToShortDateString(),t1.Text,this.song1.SelectedIndex);
                if (SetT2.Checked == true)
                {
                    database.addAlarm(this.dateTP2.Value.ToShortDateString(), t2.Text, this.song2.SelectedIndex);
                    if (SetT3.Checked == true)
                    {
                        database.addAlarm(this.dateTP3.Value.ToShortDateString(), t3.Text, this.song3.SelectedIndex);
                        if (SetT4.Checked == true)
                        {
                            database.addAlarm(this.dateTP4.Value.ToShortDateString(), t4.Text, this.song4.SelectedIndex);
                            if (SetT5.Checked == true)
                            {
                                database.addAlarm(this.dateTP5.Value.ToShortDateString(), t5.Text, this.song5.SelectedIndex);
                            }
                        }
                    }
                }
            }
            this.Close();
        }
    }
}
